﻿#region

using Microsoft.Xna.Framework;

using Pob.CustomEventArgs;

#endregion

namespace Pob.GameObjects
{
    public delegate void MenuItemEvent(object obj, MenuItemPickedEventArgs args);

    public enum MenuItemExtendedMode
    {
        String, 

        Key
    }

    public class MenuItem : DrawableGameComponent
    {
        #region Fields

        private readonly bool _extended;

        private readonly string _value;

        #endregion

        #region Constructors and Destructors

        public MenuItem(Game game, DrawString item, MenuItemEvent eventhandler)
            : base(game)
        {
            Item = item;
            MenuItemClicked += eventhandler;
        }

        public MenuItem(Game game, DrawString item, MenuItemEvent eventhandler, MenuItemExtendedMode extendedMode, string value)
            : base(game)
        {
            Item = item;

            MenuItemClicked += eventhandler;
            _extended = true;
            ExtendedMode = extendedMode;
            _value = value;

            if (_extended)
            {
                ItemExtension = (DrawString)item.Clone();
            }
        }

        #endregion

        #region Public Events

        public event MenuItemEvent MenuItemClicked;

        #endregion

        #region Public Properties

        public bool Extended
        {
            get
            {
                return _extended;
            }
        }

        public MenuItemExtendedMode ExtendedMode { get; private set; }

        public DrawString Item { get; private set; }

        public DrawString ItemExtension { get; private set; }

        #endregion

        #region Public Methods and Operators

        public override void Draw(GameTime gameTime)
        {
            Item.Draw(gameTime);

            if (_extended)
            {
                ItemExtension.Draw(gameTime);
            }

            base.Draw(gameTime);
        }

        public override void Initialize()
        {
            Item.Initialize();

            if (_extended)
            {
                ItemExtension.Initialize();
                ItemExtension.Text = _value;
                ItemExtension.Selected = false;
                ItemExtension.DrawMode = DrawStringMode.Extended;
            }

            base.Initialize();
        }

        public void OnMenuItemClicked(object obj, MenuItemPickedEventArgs args)
        {
            if (MenuItemClicked != null)
            {
                MenuItemClicked(this, args);
            }
        }

        public override void Update(GameTime gameTime)
        {
            Item.Update(gameTime);

            if (_extended)
            {
                ItemExtension.Update(gameTime);
            }

            base.Update(gameTime);
        }

        #endregion
    }
}